package com.mysql.opt;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import system.Sys_Config;
import system.queryer.MySQL_Queryer;

public abstract class Operator {
	
	public final static String nextline = "\n";
	
	protected String oprType = "";
	
	protected MySQL_Queryer mysqlQ = new MySQL_Queryer();
	
	public abstract List getObjs();	
	
	public abstract void refresh();
	
	public abstract List<String> getNames();
	
	public abstract List<String[]> getBackupSQLs();
		
	public abstract boolean drop(int index);
	
	public abstract boolean dropAll();
	
	
	public void dumpSql(String sqlLocation, String encoding){
		File fi = new File(sqlLocation);		
		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fi), encoding));
			bw.write(Operator.generateHeaderContent(this) + Operator.nextline);
			bw.flush();
			Operator.writeSql(bw, this);
			bw.close();
		} catch (Exception e ){
			e.printStackTrace();
		}
	}
	
	public static void writeSql(BufferedWriter bw, Operator optr) throws IOException{
		String sep = Sys_Config.BACKUP_CONFIG_MAP.get("SEPERATOR");
		System.out.println("Waiting Data Transfer...");
		List<String[]> backL = optr.getBackupSQLs();		
		if(optr.oprType.equalsIgnoreCase("TABLE")){
			String seperator = sep.replace("[name]", "UnLink Import Constrains").replace("[nextline]", Operator.nextline);
			bw.write(seperator);
			bw.write("SET FOREIGN_KEY_CHECKS=0;" + Operator.nextline + Operator.nextline);
		}
		for(int i=0;i<backL.size();i++){				
			String[] backI = backL.get(i);							
			String seperator = sep.replace("[name]", backI[0]).replace("[nextline]", Operator.nextline);
			bw.write(seperator + nextline);
			bw.write(backI[1] + nextline);
			System.out.println("("+(i+1)+"/"+backL.size()+")BACKUP.:" + backI[0]);		
		}
		bw.flush();
	}
	
	public static String generateHeaderContent(Operator opr){
		String header_content = "";
		header_content = Sys_Config.BACKUP_CONFIG_MAP.get("HEADER_CONTENT");		
		header_content = header_content.replace("[DriverName]", Sys_Config.BACKUP_SYS_MAP.get(Sys_Config.KEY_DRIVER_NAME));	
		header_content = header_content.replace("[DriverVer]", Sys_Config.BACKUP_SYS_MAP.get(Sys_Config.KEY_DRIVER_VER));	
		header_content = header_content.replace("[ProductName]", Sys_Config.BACKUP_SYS_MAP.get(Sys_Config.KEY_PROCDUCT_NAME));	
		header_content = header_content.replace("[ProductVer]", Sys_Config.BACKUP_SYS_MAP.get(Sys_Config.KEY_PROCDUCT_VER));
		header_content = header_content.replace("[type]", opr.oprType);	
		header_content = header_content.replace("[date]", new Date().toString());			
		return header_content;
	}
	
}  
